package com.lanou.zakerproject.community;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.VolleySingleton;
import com.lanou.zakerproject.tool.GsonRequest;

import java.util.ArrayList;

/**
 * Created by dllo on 16/1/13.
 */
public class FindFragment extends Fragment implements AdapterView.OnItemClickListener {
    private ListView listView;
    private FindAdapter findAdapter;
    private Context context;
    private TypeData typeData;
    private String url = "http://dis.myzaker.com/api/list_discussion.php?_appid=AndroidPhone&_bsize=1080_1920&_dev=97&_lat=4.9E-324&_lng=4.9E-324&_mcode=D79B3480&_net=wifi&_nudid=7891a15eb7d34771&_os=5.1_GoogleNexus5-5.1.0-API22-1080x1920&_os_name=GoogleNexus5-5.1.0-Aapi_url:PI22-1080x1920&_udid=7891a15eb7d34771&_v=6.4.5&_version=6.44&except_subscribe=N";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView) view.findViewById(R.id.community_listView_find);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getRequsetData(url);
        listView.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        String ID = typeData.getData().getList().get(position).getPk();
        intent.putExtra("ID", ID);
        intent.setClass(context, CommunityDetailActivity.class);
        startActivity(intent);
    }

    /**
     * 解析数据的方法
     *
     * @param urls 接口
     */
    private void getRequsetData(String urls) {
        GsonRequest<TypeData> gsonRequest = new GsonRequest<>(urls, TypeData.class, new Response.Listener<TypeData>() {
            @Override
            public void onResponse(TypeData response) {
                findAdapter = new FindAdapter(context, response);
                listView.setAdapter(findAdapter);
                typeData = response;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }
}
